let timecodeToSeconds = function (hh_mm_ss_ff, fps) {
    var tc_array = hh_mm_ss_ff.split(":");
    var tc_hh = parseInt(tc_array[0]);
    var tc_mm = parseInt(tc_array[1]);
    var tc_ss = parseInt(tc_array[2]);
    var tc_ff = parseInt(tc_array[3]);
    var tc_in_seconds = tc_hh * 3600 + tc_mm * 60 + tc_ss + tc_ff / fps;
    return tc_in_seconds;
}

let secondsToTimecode = function (time, fps) {
    var hours = Math.floor(time / 3600) % 24;
    var minutes = Math.floor(time / 60) % 60;
    var seconds = Math.floor(time % 60);
    var frames = Math.floor(((time % 1) * fps).toFixed(3));
    var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" +
        (seconds < 10 ? "0" + seconds : seconds) + ":" + (frames < 10 ? "0" + frames : frames);
    return result;
}

let secondsToFrameNum = function (time, fps) {
    //帧的有效时间左闭右开[start, end),帧数从1开始计算
    // n <= time*fps + 1 < n+1
    return Math.floor(time * fps + 1);
}

let frameNumToSeconds = function (frameNum, fps) {
    //帧的有效时间左闭右开[start, end),帧数从1开始计算
    // [(n - 1)/fps, n/fps)
    let start = (frameNum - 1)/fps;
    let end = frameNum/fps;
    return (start + end)/2;
}

export default { timecodeToSeconds, secondsToTimecode, secondsToFrameNum, frameNumToSeconds};